package com.alfayun.fiscalriskanalysis.enterprise;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EnterpriseService {
	
	@Autowired
	private EnterpriseRepository repository;
	
	public List<Enterprise> list() {
		return repository.findAll();
	}
	
	public Enterprise queryById(int id) throws NoSuchEnterpriseException {
		return repository.findById(id).orElseThrow(NoSuchEnterpriseException::new);
	}
	
	public Enterprise newEnterprise(Enterprise enterprise) {
		return Enterprise.newEnterprise(repository, enterprise);
	}

	public void deleteEnterprise(int id) {
		repository.deleteById(id);
	}

	public Enterprise modifyEnterprise(int id, Enterprise enterprise) throws NoSuchEnterpriseException {
		Optional<Enterprise> row = repository.findById(id);

		if (row.isPresent()) {
			return repository.save(enterprise);
		}

		throw new NoSuchEnterpriseException();
	}

}
